import type { FormSchema } from '@/components/core/schema-form/';
import { Radio, Button, message, Upload } from 'ant-design-vue';
import { UploadOutlined } from '@ant-design/icons-vue';
import { Storage } from '@/utils/Storage';
import { ACCESS_TOKEN_KEY } from '@/enums/cacheEnum';
import type { UploadChangeParam } from 'ant-design-vue';

export const bookSchemas: FormSchema<API.CreateBookParams>[] = [
  {
    field: 'name',
    component: 'Input',
    label: '模板名称',
    rules: [{ required: true }],
    componentProps: {
      maxlength: 32,
      showCount: true,
    },
  },
  {
    field: 'fileUrl',
    component: 'Input',
    label: '模板名称',
    vShow: false,
  },
  {
    field: 'file',
    component: 'Upload',
    label: '模板上传',
    rules: [{ required: true, type: 'array', message: '请上传文件' }],
    componentProps: ({ formModel, formInstance }) => {
      return {
        action: `${import.meta.env.VITE_BASE_API}/manage/upload/uploadFile/4`,
        headers: {
          'API-User-Token': Storage.get(ACCESS_TOKEN_KEY),
        },
        accept: 'application/pdf',
        maxCount: 1,
        onChange: (info: UploadChangeParam) => {
          if (info.file.status === 'done') {
            message.success(`${info.file.name}上传成功`);
            formModel.fileUrl = info.file.response.data.fileIds[0];
          } else if (info.file.status === 'error') {
            message.error(`${info.file.name}上传失败`);
          }
        },
        beforeUpload: (file, fileList) => {
          const isPDF = file.type === 'application/pdf';
          if (!isPDF) {
            message.error(`${file.name} 文件类型不是PDF`);
          }
          return isPDF || Upload.LIST_IGNORE;
        },
      };
    },
    componentSlots: {
      default: () => (
        <Button>
          <UploadOutlined /> 上传文件
        </Button>
      ),
    },
  },
];
